<template>
  <div class="login-container">
    <el-row type="flex">
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login-form"
          ref="loginForms"
          :model="loginForm"
          :rules="rules"
        >
          <h1>hello</h1>
          <h2>欢迎来到美条街总后台</h2>
          <el-form-item prop="account">
            <el-input
              v-model="loginForm.account"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              prefix-icon="el-icon-lock"
              v-model="loginForm.password"
              :show-password="true"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login-btn"
              color="#626aef"
              :loading="laoding"
              type="primary"
              size="default"
              @click="submit"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 引入获取当前时间的函数
import { getTime } from "@/utils/time.js"
import { mapState, mapActions } from "vuex"
export default {
  data() {
    return {
      laoding: false,
      loginForm: {
        account: "admin",
        password: "123456",
      },
      rules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    }
  },
  computed: {
    ...mapState("user", ["username"]),
  },
  methods: {
    ...mapActions("user", ["userLogin"]),
    async submit() {
      this.$refs.loginForms.validate(async (valid) => {
        this.laoding = true
        if (valid) {
          try {
            await this.userLogin(this.loginForm)
            // 编程式导航跳转到首页
            // 判断登录的时候，路径当中是否有query参数，如果有就往query参数跳转，没有就跳首页
            //判断登录的时候,路由路径当中是否有query参数，如果有就往query参数挑战，没有跳转到首页
            let redirect = this.$route.query.redirect
            this.$router.push({ path: redirect || "/" })
            this.$msg.successMsg(`HI,${getTime()}好`)
            this.laoding = false
          } catch (error) {
            this.laoding = false
            // 登录失败提示信息
            // this.$msg.errorMsg(error.msg)
          }
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;
  .login-form {
    position: relative;
    width: 80%;
    top: 30vh;
    padding: 40px;
    box-sizing: border-box;
    background-color: transparent;

    h1 {
      color: #fff;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: #fff;
      margin: 20px 0;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
